package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

//暴露合同货物的dubbo服务
@Service
public class ContractProductServiceImpl implements ContractProductService {

	@Autowired
	private ContractDao contractDao;  //合同Dao接口

	@Autowired
	private ContractProductDao contractProductDao; //合同货物Dao接口

	@Autowired
	private ExtCproductDao extCproductDao; //附件dao接口

	/**
	 * 保存货物
	 *    参数：货物对象，在货物对象中包含了一个属性
	 *          contractId：所属购销合同ID
	 */
	public void save(ContractProduct cp) {
		//1.计算保存货物的总金额
		double money = 0.0;
		if(cp.getPrice() != null && cp.getCnumber()!=null) {
			money = cp.getPrice() * cp.getCnumber();
		}
		cp.setAmount(money);
		//2.设置货物的ID
		cp.setId(UUID.randomUUID().toString());
		//3.保存货物
		contractProductDao.insertSelective(cp);
		//4.根据合同id，查询购销合同
		Contract contract = contractDao.selectByPrimaryKey(cp.getContractId());
		//5.设置总金额和货物数
		contract.setTotalAmount(contract.getTotalAmount() + money ); //总金额
		contract.setProNum(contract.getProNum() + 1); //货物数量
		//6.更新购销合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	/**
	 * 更新购销合同
	 *     参数：修改之后的合同对象
	 *            修改之后的单价，修改之后的数量，所属的合同id（contractId）
	 */
	public void update(ContractProduct cp) {
		//1.计算修改之后的总金额
		double newMoney = 0.0;
		if(cp.getPrice() != null && cp.getCnumber()!=null) {
			newMoney = cp.getPrice() * cp.getCnumber();
		}
		cp.setAmount(newMoney);
		//2.查询修改之前的总金额
		ContractProduct oldCp = contractProductDao.selectByPrimaryKey(cp.getId());
		double oldMoney = oldCp.getAmount();
		//3.更新数据库
		contractProductDao.updateByPrimaryKeySelective(cp);
		//4.根据购销合同id查询合同
		Contract contract = contractDao.selectByPrimaryKey(cp.getContractId());
		//5.计算购销合同的总金额
		contract.setTotalAmount(contract.getTotalAmount() - oldMoney + newMoney);
		//6.更新购销合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	/**
	 * 删除货物
	 *  参数：货物id
	 */
	public void delete(String id) {
		//1.根据id查询删除的货物对象
		ContractProduct cp = contractProductDao.selectByPrimaryKey(id);
		//2.根据货物id，查询所有的货物附件
		ExtCproductExample example = new ExtCproductExample();
		ExtCproductExample.Criteria criteria = example.createCriteria();
		criteria.andContractProductIdEqualTo(id);
		List<ExtCproduct> extcs = extCproductDao.selectByExample(example);
		//3.删除货物
		contractProductDao.deleteByPrimaryKey(id);
		//4.删除货物的所有附件
		//5.计算删除的总金额
		double money = cp.getAmount();
		for (ExtCproduct extc : extcs) {
			extCproductDao.deleteByPrimaryKey(extc.getId());
			money+= extc.getAmount();
		}
		//6.根据购销合同id查询购销合同
		Contract contract = contractDao.selectByPrimaryKey(cp.getContractId());
		//7.设置购销合同总金额
		contract.setTotalAmount(contract.getTotalAmount() - money);
		//8.设置附件、货物数量
		contract.setProNum(contract.getProNum() - 1 );
		contract.setExtNum(contract.getExtNum() - extcs.size());
		//9.更新购销合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	@Override
	public ContractProduct findById(String id) {
		return contractProductDao.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo findAll(int page, int size, ContractProductExample example) {
		PageHelper.startPage(page,size);
		List<ContractProduct> list = contractProductDao.selectByExample(example);
		return new PageInfo(list);
	}

	//保存货物列表
	public void saveAll(List<ContractProduct> list) {
		if(list != null && list.size()>0) {
			for (ContractProduct contractProduct : list) {
				save(contractProduct);
			}
		}
	}
}
